<?php


namespace app\xcxapi\service;

use app\common\service\BaseService;

class TimetableService extends BaseService
{
    /**
     * User: wufeng
     * Date: 2020/02/20.
     *
     * @param $params
     *
     * @return array
     */
    public function timetableIndex($params)
    {
        try {
            $page = isset($params['page']) ? (int) $params['page'] : 1;
            $listRows = isset($params['limit']) ? (int) $params['limit'] : 10;
            $where = [];
            if (isset($params['word']) && '' !== $params['word']) {
                $where['class_grades_name|class_room|teacher'] = ['like', $params['word'].'%'];
            }
            $where['graduation_status'] = 0; //未结业
            //课程id
            if (isset($params['course_id']) && '' !== $params['course_id']) {
                $where['course_id'] = $params['course_id'];
            }

            $where['campus_id'] = 1;
            //			$where['campus_id'] = get_current_campus_id();

            //时间段
            if (isset($params['start_date']) && '' !== $params['start_date'] && isset($params['end_date']) && '' !== $params['end_date']) {
                $where['class_date'] = ['between', [$params['start_date'], $params['end_date']]];
            }

            $this->data = (new GradesDetailsListsModel())->where($where)->order('update_time desc')
                ->paginate($listRows, false, ['page' => $page])
                ->toArray();
            if (!empty($this->data)) {
                $i = 0;
                foreach ($this->data['data'] as &$item) {
                    $i = $i + 1;
                    $item['course_name'] = (new CourseModel())->get($item['course_id'])['course_name'];
                    $item['campus_name'] = (new CampusModel())->get($item['campus_id'])['campus_name'];
                    $res = (new GradesDetailsModel())->get($item['class_manage_id']);
                    $item['course_class_name'] = $item['course_name'].'第'.$i.'节';

                    $start = strtotime($item['class_subject_start']);  //开始时间
                    $end = strtotime($item['class_subject_end']);  //结束时间
                    $item['course_date_week_time'] = $item['class_date'].'-'.$item['weekday'].'-'.$item['class_subject_start'].'-'.$item['class_subject_end'];
                    $item['class_length'] = ceil(($end - $start) / 60).'分钟';
                    $nums = (new StudentSubjectInfosClassModel())->where(['class_manage_details_id' => $item['id']])->select();
                    if (0 === $item['status']) {  //未上课
                        $item['actual_number'] = '0/'.\count($nums);
                    } elseif (1 === $item['status']) {  //已上课
                        $already_nums = 0;
                        foreach ($nums as $key => $value) {
                            if (1 === $value['attendance']) {
                                ++$already_nums;
                            }
                        }
                        $item['actual_number'] = $already_nums.'/'.\count($nums);
                    }
                    $item['student_nums'] = $res['preoccupation'];
                    $result = (new StudentSubjectClassModel())->where(['class_subject_id' => $item['class_manage_id']])->select();
                    $student_id_arr = [];
                    $nums = 0;
                    foreach ($result as $key => $value) {
                        if (!\in_array($value['student_id'], $student_id_arr, true)) {
                            $nums = $nums + 1;
                            $student_id_arr[] = $value['student_id'];
                        }
                    }

                    if (0 === $item['student_nums']) {
                        $item['student_nums'] = $nums.'/'.'不限';
                    } else {
                        $item['student_nums'] = $nums.'/'.$item['student_nums'];
                    }
                }
            }
            if (empty($this->data)) {
                throw new BaseException('NO_DATA');
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/02/20.
     *
     * @param $params
     *
     * @return array
     */
    public function timetableLists($params)
    {
        try {
            $page = isset($params['page']) ? (int) $params['page'] : 1;
            $listRows = isset($params['limit']) ? (int) $params['limit'] : 10;
            $where = [];
            if (isset($params['word']) && '' !== $params['word']) {
                $where['class_grades_name|class_room|teacher'] = ['like', $params['word'].'%'];
            }
            $where['graduation_status'] = 0; //未结业
            //课程id
            if (isset($params['course_id']) && '' !== $params['course_id']) {
                $where['course_id'] = $params['course_id'];
            }

            $where['campus_id'] = 1;
            //			$where['campus_id'] = get_current_campus_id();

            //时间段
            if (isset($params['start_date']) && '' !== $params['start_date'] && isset($params['end_date']) && '' !== $params['end_date']) {
                $where['class_date'] = ['between', [$params['start_date'], $params['end_date']]];
            }

            $this->data = (new GradesDetailsListsModel())->where($where)->order('update_time desc')
                ->paginate($listRows, false, ['page' => $page])
                ->toArray();
            if (!empty($this->data)) {
                $i = 0;
                foreach ($this->data['data'] as &$item) {
                    $i = $i + 1;
                    $item['course_name'] = (new CourseModel())->get($item['course_id'])['course_name'];
                    $item['campus_name'] = (new CampusModel())->get($item['campus_id'])['campus_name'];
                    $res = (new GradesDetailsModel())->get($item['class_manage_id']);
                    $item['course_class_name'] = $item['course_name'].'第'.$i.'节';

                    $start = strtotime($item['class_subject_start']);  //开始时间
                    $end = strtotime($item['class_subject_end']);  //结束时间
                    $item['course_date_week_time'] = $item['class_date'].'-'.$item['weekday'].'-'.$item['class_subject_start'].'-'.$item['class_subject_end'];
                    $item['class_length'] = ceil(($end - $start) / 60).'分钟';
                    $nums = (new StudentSubjectInfosClassModel())->where(['class_manage_details_id' => $item['id']])->select();
                    if (0 === $item['status']) {  //未上课
                        $item['actual_number'] = '0/'.\count($nums);
                    } elseif (1 === $item['status']) {  //已上课
                        $already_nums = 0;
                        foreach ($nums as $key => $value) {
                            if (1 === $value['attendance']) {
                                ++$already_nums;
                            }
                        }
                        $item['actual_number'] = $already_nums.'/'.\count($nums);
                    }
                    $item['student_nums'] = $res['preoccupation'];
                    $result = (new StudentSubjectClassModel())->where(['class_subject_id' => $item['class_manage_id']])->select();
                    $student_id_arr = [];
                    $nums = 0;
                    foreach ($result as $key => $value) {
                        if (!\in_array($value['student_id'], $student_id_arr, true)) {
                            $nums = $nums + 1;
                            $student_id_arr[] = $value['student_id'];
                        }
                    }

                    if (0 === $item['student_nums']) {
                        $item['student_nums'] = $nums.'/'.'不限';
                    } else {
                        $item['student_nums'] = $nums.'/'.$item['student_nums'];
                    }
                }
            }
            if (empty($this->data)) {
                throw new BaseException('NO_DATA');
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }
}
